﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class AlloDetController : Controller
    {
        //
        // GET: /AlloDet/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSChiTietPhuCap
                            join ll in context.DSPhuCap on dt.IDPhuCap equals ll.ID
                            where dt.IDCanBo == id
                            select new { ll.Ten, ll.HeSo, dt.IDPhuCap, dt.ID }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.IDPhuCap,
                                 customer.Ten,
                                 customer.HeSo
                                
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id = 0)
        {
            List<ChiTietPhuCap> dsqhgd = ChiTietPhuCapModels.GetChiTietPhuCapTheoCanBo(id);
            ViewBag.DSChiTietPhuCap = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {
            List<tblLoaiPhuCap> dsloaiphucap = LoaiPhuCapModels.DSLoaiPhuCap();
            ViewBag.DSLoaiPhuCap = dsloaiphucap;
            List<PhuCap> dsll = PhuCapModels.GetDSPhuCap();
            ViewBag.DSPhuCap = dsll;
            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            ChiTietPhuCap themKyLuat = new ChiTietPhuCap();

            themKyLuat.IDPhuCap = Convert.ToInt32(form["IDPhuCap"]);
          
            themKyLuat.IDCanBo = id;
            //themQHGD.IDCanBo = id;

            ChiTietPhuCapModels.Add(themKyLuat);
            //return RedirectToAction("Index");
            return RedirectToAction("Index", "AlloDet", new { id = themKyLuat.IDCanBo });
        }

        public ActionResult Update(int id)
        {
            List<PhuCap> dsll = PhuCapModels.GetDSPhuCap();
            ViewBag.DSPhuCap = dsll;

            ChiTietPhuCap listQHGD = ChiTietPhuCapModels.GetChiTietPhuCap(id);
            ViewBag.DSChiTietPhuCap = listQHGD;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            ChiTietPhuCap s = ChiTietPhuCapModels.GetChiTietPhuCap(id);
            //s.IDCanBo = Convert.ToInt32(form["IDCanBo"]);
            s.IDPhuCap = Convert.ToInt32(form["IDPhuCap"]);
            
            ChiTietPhuCapModels.Update(s);
            return RedirectToAction("Index", "AlloDet", new { id = s.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            ChiTietPhuCap s = ChiTietPhuCapModels.GetChiTietPhuCap(id);
            ChiTietPhuCapModels.Delete(id);

            return RedirectToAction("Index", "AlloDet", new { id = s.IDCanBo });
        }

        public JsonResult GetDanhSachPhuCap(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            Dictionary<int, string> dsnn = new Dictionary<int, string>();
            var query = (from phucap in context.DSPhuCap
                         where phucap.IdLoaiPhuCap == id
                         select phucap).ToList();
            foreach (var l in query)
            {
                dsnn.Add(l.ID, l.Ten);
            }
            return Json(dsnn.ToArray(), JsonRequestBehavior.AllowGet);
        }
    }
}
